{include file=member.header.html}
{include file="title.html"}
<form method="post" id="order_form">
<!--order.shipping.html script_start-->
<script type="text/javascript" src="{lib file=mlselection.js}" charset="utf-8"></script>
<script type="text/javascript" src="{lib file=jquery.plugins/jquery.validate.js}" charset="utf-8"></script>
<script type="text/javascript" src="{lib file=dialog/dialog.js}" id="dialog_js" charset="utf-8"></script>
<script type="text/javascript" src="{lib file=jquery.ui/jquery.ui.js}" id="dialog_js" charset="utf-8"></script>
<script type="text/javascript">
                var addresses = {$delivery_data.addresses};
                var all_regions={$all_regions};
                var shippings = new Array();
                var goods_amount = new Array();
                var goods_quantity = new Array();
                
                var total_integral_max_exchange = new Array();
                var order_amount=new Array(); //�¼�
                var store_integral=new Array();
                var is_free_fee=new Array();
                
                var addresses_array;
                addresses_array=eval(addresses);
                
                var member_integral={$member_info.integral|default:0};//新加
                var integral_enabled={$integral_enabled|default:0};
                $(function(){
                    regionInit("region");
                    $('#order_form').validate({
            invalidHandler: function(e, validator) {
                var err_count = validator.numberOfInvalids();
                alert(lang.order_information + err_count + lang.red_italics_section);
                /*  var msg_tpl = '{$lang.order_have_error}';
                 var d = DialogManager.create('show_error');
                 d.setWidth(400);
                 d.setTitle(lang.error);
                 d.setContents('message', {type:'warning', text:$.format(msg_tpl, err_count)});
                 d.show('center');*/
            },
            errorPlacement: function(error, element) {
                var _message_box = $(element).parent().find('.field_message');
                _message_box.find('.field_notice').hide();
                _message_box.append(error);
            },
            success: function(label) {
                //label.addClass('validate_right').text('OK!');
            },
                        rules : {
                            consignee : {
                                required : true
                            },
                            region_id : {
                                required : true,
                                min   : 1
                            },
                            address   : {
                                required : true
                            },
                            phone_mob : {
                                required : check_phone,
                                minlength:6,
                                digits : true
                            }
                        },
                        messages : {
                            consignee : {
                                required : '{$lang.consignee_required}'
                            },
                            region_id : {
                                required : '{$lang.region_required}',
                                min  : '{$lang.region_required}'
                            },
                            address   : {
                                required : '{$lang.address_required}'
                            },
                            phone_mob : {
                                required : '{$lang.phone_required}',
                                minlength: '{$lang.phone_mob_error}',
                                digits : '{$lang.phone_mob_error}'
                            }
                        }
                    });

                    $('ul[shipping_id]').each(function(){
                        var store_id=$(this).attr('store_id');
                        var _shipping_fee = get_shipping_fee($(this).attr('shipping_id'),store_id);
                        $(this).find('[ectype="shipping_fee"]').html(price_format(_shipping_fee));
                    }).click(function(){
                        $(this).parent().find(".shipping_info").hide();
                        $(this).find(".shipping_info").show();
                        
                        var store_id=$(this).attr('store_id');
                        $(this).find('input[name="shipping_id_'+store_id+'"]').attr('checked', true);

                        if(integral_enabled && total_integral_max_exchange[store_id]>0 && member_integral>0){
                            set_order_amount_total(store_id);
                        }else{
                            set_order_amount(store_id);
                        }                    

                    });

                });
                function set_order_amount(store_id){
                    var shipping_id = $('input[name="shipping_id_'+store_id+'"]:checked').val();
                    var _shipping_fee=get_shipping_fee(shipping_id,store_id);
                    var _amount  = ForDight((goods_amount[store_id] + _shipping_fee),2);
                    order_amount[store_id]=_amount;
                    $('#order_amount_'+store_id).html(price_format(_amount));
                    var total_orders_amount=0;
                    for(var key in order_amount){
                        total_orders_amount=ForDight((total_orders_amount+order_amount[key]),2);
                    }
                    $('#total_orders_amount').html(price_format(total_orders_amount));
                }
                function set_order_amount_total(store_id){
                    var shipping_id = $('input[name="shipping_id_'+store_id+'"]:checked').val();
                    var _shipping_fee=get_shipping_fee(shipping_id,store_id);
					var coupon_sn_money = 0;
                    var coupon_sn =document.getElementById("coupon_sn_"+store_id+"");
                    var index=coupon_sn.selectedIndex ;
                    var coupon_sn_text=  coupon_sn.options[index].text;
                    var coupon_sn_text = /[^\(\)]+(?=\))/g.exec(coupon_sn_text);
                    var coupon_sn_text = String(coupon_sn_text);
                    var coupon_sn_text =parseFloat(coupon_sn_text.replace("¥",""));
                    if(coupon_sn_text){
                         coupon_sn_money = coupon_sn_text;
                    }
                    var min_integral;
                    //当前设置积分数
                    var use_integral  = parseInt($('#use_integral_'+store_id).val());
                    //当设置积分为负数时
                    if(use_integral<0 || isNaN(use_integral)){
                        $('#use_integral_'+store_id).val('');
                        use_integral=0;
                    }
                    //integral_start
                    //找到最小的
                    member_integral={$member_info.integral|default:0};
                    for(var key in store_integral){
                        if(key != store_id){
                            member_integral=member_integral-store_integral[key];
                        }
                    }    
                    min_integral=member_integral;
                    if(total_integral_max_exchange[store_id]<min_integral){
                        min_integral=total_integral_max_exchange[store_id];
                        
                    }
                    if({$integral_seller|default:0}){
                        var max_goods_integray=parseInt(goods_amount[store_id]/{$integral_seller|default:0});
                        if(max_goods_integray<min_integral){
                            min_integral=max_goods_integray;
                            
                        }
                    }
                    if(use_integral>min_integral){
                        use_integral=min_integral;
                    }
                    //$("body").prepend(use_integral+'B<br>');
                    //integral_end
               
                        store_integral[store_id]=use_integral;
                        if(use_integral){
                            $('#use_integral_'+store_id).val(use_integral);
                        }else{
                            $('#use_integral_'+store_id).val('');
                        }
                        
                         
                        //积分兑换比例 得出具体抵扣数值
                        var _integral_fee = use_integral*{$integral_seller|default:0};
                        var _amount  = goods_amount[store_id] + _shipping_fee - _integral_fee - coupon_sn_money;
                        order_amount[store_id]=_amount;
                        $('#order_amount_'+store_id).html(price_format(ForDight(_amount,2)));
                        
                    var total_orders_amount=0;
                    for(var key in order_amount){
                        total_orders_amount=ForDight((total_orders_amount+order_amount[key]),2);
                    }
                    $('#total_orders_amount').html(price_format(total_orders_amount));
                }
//四舍五入函数
function   ForDight(Dight,How)  
{  
            Dight   =   Math.round   (Dight*Math.pow(10,How))/Math.pow(10,How);  
            return   Dight;  
}  
                
                function get_shipping_fee(shipping_id,store_id){
                    if(!shipping_id){
                        return 0;
                    }
                    var shipping_data = shippings[store_id][shipping_id];
                    var first_price   = Number(shipping_data['first_price']);
                    var step_price   = Number(shipping_data['step_price']);
                    
                    var cod_regions_price=shipping_data['cod_regions_price'];


                  //获取地区价格
                  var ajex_region_id;

                  if(parseInt($("input[name='address_options']:checked").val())>0){
                  ajex_region_id=addresses_array[$("input[name='address_options']:checked").val()]['region_id'];
                  }else{
                  if($("#region_id").val()){
                      ajex_region_id=$("#region_id").val();
                  }
                  }

                  if(ajex_region_id){
                  	var suitble_region=0;
                  	
                  for(var r_p in cod_regions_price){
                  	
                  	var region_price_array;
                    if(ajex_region_id==r_p){
                        
        
                        region_price_array=cod_regions_price[r_p].split(",");
                       
                        first_price = parseFloat(region_price_array[1]);
                        step_price   = parseFloat(region_price_array[2]);
                        
                            break;
                       
                    }
                    $.ajaxSettings.async = false; 
                   //获取地区的子孙
                   if(typeof(all_regions[ajex_region_id])!='undefined'){
                       if($.inArray(r_p,all_regions[ajex_region_id])>-1){
                                  region_price_array=cod_regions_price[r_p].split(",");

                                  if((suitble_region==0) || (typeof(all_regions[r_p])!='undefined' && (all_regions[r_p].length >suitble_region)))
                                  {
                                      suitble_region=all_regions[r_p].length;
                        first_price = parseFloat(region_price_array[1]);
                        step_price   = parseFloat(region_price_array[2]);
                        
                                  }
                       }
                   }
                   

                      
                      
                      
                  }

                  }
                  var _shipping_fee=first_price + (goods_quantity[store_id] - 1) * step_price;
                  if(shipping_id){
                		$('ul[shipping_id='+shipping_id+']').find('[ectype="shipping_fee"]').html(price_format(_shipping_fee));
                	}
                	return _shipping_fee;
                }
                function check_phone(){
                    return ($('#phone_mob').val() == '');
                }
                function hide_error(){
                    $('#region').find('.error').hide();
                }
                </script>
<script type="text/javascript">
                //<![CDATA[
                $(function(){
                    //$("input[name='address_options']").click(set_address);
                    $('.address_item').click(function(){
                        $(this).find("input[name='address_options']").attr('checked', true);
                        $('.address_item').removeClass('selected_address');
                        $(this).addClass('selected_address');
                        set_address();
                    });
                    //init
                    set_address();
                    

                    $("input[name='address_options']").change(function(){
                        <!--{foreach from=$goods_info_array item=goods_info key=key }-->
                            <!--{if $integral_enabled && $goods_info.total_integral_max_exchange gt 0 && $member_info.integral gt 0}-->
                            set_order_amount_total({$key});
                            <!--{else}-->
                            set_order_amount({$key});
                            <!--{/if}-->
                        <!--{/foreach}-->
                                }); 
                    $(".address_item").click(function(){
                        <!--{foreach from=$goods_info_array item=goods_info key=key }-->
                            <!--{if $integral_enabled && $goods_info.total_integral_max_exchange gt 0 && $member_info.integral gt 0}-->
                            set_order_amount_total({$key});
                            <!--{else}-->
                            set_order_amount({$key});
                            <!--{/if}-->
                        <!--{/foreach}-->
                                }); 
                    $("#region").click(function(){
                        <!--{foreach from=$goods_info_array item=goods_info key=key }-->
                            <!--{if $integral_enabled && $goods_info.total_integral_max_exchange gt 0 && $member_info.integral gt 0}-->
                            set_order_amount_total({$key});
                            <!--{else}-->
                            set_order_amount({$key});
                            <!--{/if}-->
                        <!--{/foreach}-->
                                });  
                    $("input[name='address_options']:checked").change();
                    
                				
                });
                function set_address(){
                    var addr_id = $("input[name='address_options']:checked").val();
                    if(addr_id == 0)
                    {
                        $('#consignee').val("");
                        $('#region_name').val("");
                        $('#region_id').val("");
                        $('#region select').show();
                        $("#edit_region_button").hide();
                        $('#region_name_span').hide();
                        $('#address').val("");
                        $('#zipcode').val("");
                        $('#phone_mob').val("");
                        $('#address_form').show();
                    }
                    else
                    {
                        $('#address_form').hide();
                        fill_address_form(addr_id);
                    }
                }
                function fill_address_form(addr_id){
                    var addr_data = addresses[addr_id];
                    for(k in addr_data){
                        switch(k){
                            case 'consignee':
                            case 'address':
                            case 'zipcode':
                            case 'email':
                            case 'phone_mob':
                            case 'region_id':
                                $("input[name='" + k + "']").val(addr_data[k]);
                            break;
                            case 'region_name':
                                $("input[name='" + k + "']").val(addr_data[k]);
                                $('#region select').hide();
                                $('#region_name_span').text(addr_data[k]).show();
                                $("#edit_region_button").show();
                            break;
                        }
                    }
                }
                //]]>
              </script>
<!--order.shipping.html script_end--> 
    
    <!---------收货地址---------->
<div class="address">
    <!--{if $delivery_data.my_address}-->
    <!--{foreach from=$delivery_data.my_address item=address name=address_select}-->
    <div class="address_info address_item{if $address.is_default} selected_address{/if}">
        <lable style="display:block;">
            <input type="radio" class="radio" {if $address.is_default}checked="checked" {/if} name="address_options" value="{$address.addr_id}" id="address_{$address.addr_id}"/>
            <span>{$lang.consignee}:{$address.consignee|escape}</span>
            <span class="dt">（<!--{if $address.phone_mob}-->{$address.phone_mob}<!--{/if}-->）</span>
            <span class="dt">{$address.region_name|escape}&nbsp;&nbsp;{$address.address|escape}</span>
            <a href="{url app=my_address}"><b>></b></a>
        </lable>
    </div>
    <!--{/foreach}-->
	<!--{else}-->
	<script>
	$(function(){
		$("#use_new_address").click();
	})
	</script>
    <!--{/if}-->
    <div  class="address_info new address_item">
        <p><input  id="use_new_address" type="radio" value="0" class="radio"  name="address_options"/>{$lang.use_new_address}</p>
        <!-------使用新地址------->
    </div>
    <div id="address_form"  class="add_box" style="margin:-4px 10px 0;padding: 5px 0 0 15px;background:#fff;border-radius:0 0 5px 5px;position: relative;">
        <p><input type="text" name="consignee" id="consignee" placeholder="{$lang.consignee_name}"/><span class="field_message explain"></span></p>
        <p><input type="text"  name="phone_mob" id="phone_mob"placeholder="{$lang.fill_in_phone_number}" /><span class="field_message explain"></span></p>
        <p id="region">
            <span style="display:none;" id="region_name_span"></span>
            <input id="edit_region_button" type="button" class="edit_region" value="{$lang.edit}" style="display:none;" />
            <select  onchange="hide_error();">
                <option value="0">--{$lang.region_empty}--</option>
                {html_options options=$delivery_data.regions}
            </select>
            <input type="hidden" class="mls_id" name="region_id" id="region_id"/><input type="hidden" name="region_name" class="mls_names" id="region_name"/>
            <span class="field_message "></span>
        </p>
        <p>
            <input type="text" name="address" id="address" placeholder="{$lang.not_necessary}" />
            <span class="field_message explain"></span>
        </p>
        <!--   <p><input type="text" name="zipcode" id="zipcode" placeholder="邮政编码"/></p>-->
        <p><input type="checkbox" value="1" id="save_address" name="save_address"  class="mr"/>{$lang.save_address}</p>
    </div>
</div>
<!---------收货地址结束---------->
<style>
    #region select{margin-top:10px;}
</style>

<!--{foreach from=$goods_info_array item=goods_info key=key }-->
    {include file="order.shipping.html"}
    <div class="orderlist">
        <ul>
            <li>{$lang.postscript}</li>
            <li>  <textarea  class="com_text" name="postscript[{$key}]" placeholder="{$lang.postscript_desc}"></textarea></li>
        </ul>
    </div>

    <div class="orderlist">
        <ul>
            <li>{$lang.store}：<a href="{url app=store&id=$goods_info.store_id}" >{$goods_info.store_name|escape}</a></li>
            <!--{foreach from=$goods_info.items item=goods}-->
            <li>
                <a  href="{url app=goods&id=$goods.goods_id}">	<img src="{$site_url}/{$goods.goods_image}" /></a>
                <p>  <a  href="{url app=goods&id=$goods.goods_id}">{$goods.goods_name|escape}</a></p>
                <p>{$lang.unit_price}：{$goods.price|price}</p>
                <p>{$lang.quantity}：{$goods.quantity}{$lang.goods_unit}<strong>{$goods.subtotal|price}</strong></p>
            </li>
            <!--{/foreach}-->
            <span style="margin-left:10px;"> {$lang.delivery_method}：</span>
            <div id="select-shipping_{$key}">
            <!--{if $goods_info.is_free_fee}-->
            <ul class="shipping_item">
                <li>
                    <input type="radio" name="is_free_fee_{$key}" checked="checked" value="1" />
                    {$goods_info.shipping_data.shipping_method.shipping_name|escape}
                    <input type="hidden" name="is_free_fee_{$key}" value="1" />
                    {$lang.shipping_fee}{$lang.colon}<span class="money" ectype="shipping_fee">&yen; 0.00</span>({$goods_info.free_fee_name|escape})
                </li>
            </ul>
            <!--{else}--> 
            <!--{foreach from=$goods_info.shipping_data.shipping_methods item=shipping_method name=shipping_select}-->
            <ul class="shipping_item" shipping_id="{$shipping_method.shipping_id}" store_id="{$key}">
                <li>
                    <input type="radio" name="shipping_id_{$key}" value="{$shipping_method.shipping_id}" />
                    {$shipping_method.shipping_name|escape}
                    <b class="shipping_info" style="display: none">{$lang.shipping_fee}:&nbsp;<span class="money" ectype="shipping_fee">&yen; 0.00</span></b>
                </li>
            </ul>
            <!--{/foreach}-->
            <!--{/if}-->
            </div>
            
            <!--{if $integral_enabled && $goods_info.total_integral_max_exchange gt 0 && $member_info.integral gt 0}-->
            <li>{$lang.avaliable_integral}<em>{$member_info.integral}</em>,</li>
            <li>{$lang.more_use_integral}<em >{$goods_info.total_integral_max_exchange}</em></li>
            <li>{$lang.integral_ratio}<em>{$integral_seller|default:0}</em></li>
            <li>{$lang.use_integral}：<input type="text" name="use_integral[{$key}]" id="use_integral_{$key}" onkeyup="set_order_amount_total({$key});" class="text" /></li>
            <!--{/if}-->
            <li>
                {if $goods_info.allow_coupon}
                <select id="coupon_sn_{$key}" name="coupon_sn[{$key}]" onchange="set_order_amount_total({$key});"  >
                    <option value="">{$lang.pls_coupon}</option>
                    {foreach from=$goods_info.coupon_list item=coupon}
                    <option value="{$coupon.coupon_sn}">SN:{$coupon.coupon_sn}({$coupon.coupon_value|price})</option>
                    {/foreach}
                </select>
                {/if}
            </li>

            <li>{$lang.total}：<strong id="order_amount_{$key}">{$goods_info.amount|price}</strong></li>
        </ul>

    </div>
<!--{/foreach}-->
                <label style="margin: 10px;">
                    <input type="checkbox" name="pd_amount" value="{$member_info.money}">
                    {$lang.use_pd_amount}（{$lang.account_cur_money}：{$member_info.money}{$lang.yuan}）
                </label>
    <p class="total_price">{$lang.actual_payment}：<strong id="total_orders_amount"></strong></p>
    <div style="margin: 0 10px 10px;">
        <a href="javascript:void($('#order_form').submit());" class="red_btn">{$lang.submit_order}</a>
    </div>
</form>
{include file="footer.html"}